package C8_14_ReplaceTypeCodeWithSubClass;

import org.junit.Test;

/**
 * @author xufy
 * @version 1.0
 * @description 以子类取代类型码
 **/
public class ReplaceTypeCodeWithSubclasses {

    /**
     * 重构前
     */
    @Test
   public void test1(){
        Employee employee = new Employee(Employee.ENGINEER);
        System.out.println("重构前："+employee.getType());
    }

    /**
     * 重构后
     */
    @Test
    public void test2(){
        Employee2 employee2 = Employee2.create(Employee2.ENGINEER);
        System.out.println("重构后："+employee2.getType());
    }

}
class Employee{

    private int _type;
    static final int ENGINEER=0;
    static final int SALESMAN=1;
    static final int MANAGER=2;

    Employee(int type){
        _type=type;
    }
    int getType(){
        return _type;
    }
}

/**
 * 重构后
 */
class Employee2{

    private int _type;
    static final int ENGINEER=0;
    static final int SALESMAN=1;
    static final int MANAGER=2;


    int getType(){
        return _type;
    }

    static Employee2 create(int type){
        switch(type){
            case ENGINEER:
                return new Engineer();
            case SALESMAN:
                return new SaleMan();
            case MANAGER:
                return new Manager();
            default:
                throw new IllegalArgumentException("Incorrect type code value");
        }
    }
    public Employee2(){


    }
    private Employee2(int type){
        _type=type;
    }

}
class Engineer extends  Employee2{

    @Override
    int getType(){
        return Employee2.ENGINEER;

    }

}
class SaleMan extends Employee2{
    @Override
    int getType(){
        return Employee2.SALESMAN;

    }
}
class Manager extends Employee2{
    @Override
    int getType(){
        return Employee2.MANAGER;
    }
}